import 'package:ccshop/eventbus/EventBus.dart';
import 'package:ccshop/utils/Constants.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginPage extends StatelessWidget {
  String _account = "";
  String _psd = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: const LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [Color(0xFFFFEBEE), Color(0xFFEF9A9A)]),
        ),
        child: Stack(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "Welcome",
                  style: TextStyle(fontSize: 30, color: Colors.white),
                ),
                Container(
                  decoration: BoxDecoration(
                    color: Color(0x33757575),
                    borderRadius: BorderRadius.circular(30),
                  ),
                  margin: EdgeInsets.only(left: 30, right: 30, top: 40),
                  padding: EdgeInsets.only(
                    left: 10,
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.account_box,
                        color: Colors.red,
                      ),
                      Padding(
                        padding: EdgeInsets.only(left: 10),
                      ),
                      Expanded(
                        child: TextField(
                          maxLines: 1,
                          cursorColor: Colors.grey,
                          // 光标颜色
                          style: TextStyle(
                            color: Colors.black,
                          ),
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: "请输入账号",
                            hintStyle: TextStyle(color: Color(0xFF616161)),
                          ),
                          onChanged: (value) {
                            _account = value;
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 20),
                ),
                Container(
                  decoration: BoxDecoration(
                    color: Color(0x33757575),
                    borderRadius: BorderRadius.circular(30),
                  ),
                  margin: EdgeInsets.only(left: 30, right: 30),
                  padding: EdgeInsets.only(
                    left: 10,
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.lock,
                        color: Colors.red,
                      ),
                      Padding(
                        padding: EdgeInsets.only(left: 10),
                      ),
                      Expanded(
                        child: TextField(
                          maxLines: 1,
                          obscureText: true,
                          cursorColor: Colors.grey,
                          // 光标颜色
                          style: TextStyle(
                            color: Colors.black,
                          ),
                          decoration: InputDecoration(
                            border: InputBorder.none,
                            hintText: "请输入密码",
                            hintStyle: TextStyle(color: Color(0xFF616161)),
                          ),
                          onChanged: (value) {
                            _psd = value;
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 50),
                ),
                // 登录按钮
                Container(
                  width: double.infinity,
                  height: 44,
                  margin: EdgeInsets.only(left: 30, right: 30),
                  child: RaisedButton(
                    onPressed: () {
                      _loginAccount(context);
                    },
                    color: Colors.red,
                    splashColor: Color(0xFFEF9A9A),
                    highlightColor: Color(0xFFEF9A9A),
                    textColor: Colors.white,
                    elevation: 2,
                    child: Text(
                      "登录",
                      style: TextStyle(fontSize: 16, letterSpacing: 6),
                    ),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20.0)), //圆
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 10),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "忘记密码？",
                      style: TextStyle(color: Color(0xFFEEEEEE)),
                    ),
                    Container(
                      width: 2,
                      height: 14,
                      margin: EdgeInsets.only(left: 5, right: 10),
                      color: Color(0xFFEEEEEE),
                    ),
                    Text(
                      "立即注册",
                      style: TextStyle(color: Color(0xFFEEEEEE)),
                    ),
                  ],
                )
              ],
            ),
            Positioned(
              left: 20,
              top: 60,
              child: InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _loginAccount(BuildContext context) async {
    if (_account.isEmpty) {
      Fluttertoast.showToast(
        msg: "请输入账号",
        gravity: ToastGravity.CENTER,
        backgroundColor: Colors.grey,
        textColor: Colors.white,
      );
      return;
    }
    if (_psd.isEmpty) {
      Fluttertoast.showToast(
        msg: "请输入密码",
        gravity: ToastGravity.CENTER,
        backgroundColor: Colors.grey,
        textColor: Colors.white,
      );
      return;
    }
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString(Constants.USER_INFO_ACCOUNT, _account);
    Navigator.pop(context);

    bus.emit(Constants.EVENT_LOGIN_ACCOUNT, _account);
  }
}
